import {reactive, ref, Ref} from "vue";
import axios from '@/utils/axios';
import {message} from "ant-design-vue";
import EveryPageEnum from "@/enums/everyPageEnum";


class WaterDeviceFormState{
    sectionId: any = undefined
    status: any = 0
    deviceName: any = undefined
    deviceSn: any = undefined
    startPage: any = 1
    everyPage: any = EveryPageEnum.small
}

class WaterInfo{
    deviceId:number|null = null
    deviceName:any = null
    sn:any = null
    gtmTime:any = null
    sectionName:any = null
    companyName:any = null
    latitude:any = null
    longitude:any = null
    // 平台里程（洒水的）
    buildTotle:any = null
    // 今日里程 （洒水的）
    buildToday:any = null
    // 平台里程（所有的）
    mileTotle:any = null
    // 今日里程 （所有的）
    mileToday:any = null
    // 总洒水方量
    waterTotle:any = null
    // 今日洒水方量
    waterToday:any = null
    // 洒水阈值(m³/s)
    threshold:number|null = null
}

export default ()=>{
    const waterViewInfo = ref({
        showModal: false
    })

    const waterFormState = ref(new WaterDeviceFormState());

    const waterList:any = ref({});

    const waterInfo = <Ref<WaterInfo>>ref(new WaterInfo());

    /**
     * 查找洒水车列表
     * @param callback
     */
    const getWaterList = (pageInfo:any|null = null)=>{
        let queryParms:any = {}
        if(pageInfo && !(pageInfo instanceof MouseEvent)){ // 若为分页跳转
            Object.assign(queryParms, waterList.value.queryParms)
            queryParms.startPage = pageInfo.current
        }else{
            Object.assign(queryParms, waterFormState.value)
        }
        waterList.value = new Array<any>();
        return new Promise((resolve,reject)=>{
            axios.post("/water/findDevice",queryParms).then((res:any)=>{
                if(res.success){
                    waterList.value = res.data
                    waterList.value.queryParms = queryParms
                    resolve(res)
                }else{
                    message.info(res.msg)
                    reject(false)
                }
            })
        })
    }

    /**
     * 查找洒水车详细信息
     * @param deviceId
     * @param callback
     */
    const getWaterInfo = (deviceId:number,callback:Function|null = null)=>{
        waterInfo.value.deviceId = deviceId
        axios.post("/water/findDeviceParameterByDeviceId",{deviceId:deviceId}).then((res:any)=>{
            if(res.success)
                Object.assign(waterInfo.value,res.data)
            if(callback) callback()
        })
    }

    /**
     * 设置洒水车详细信息
     * @param callback
     */
    const setWaterInfo = (callback:Function|null=null)=>{
        axios.post("/water/saveDeviceParameterByDeviceId",waterInfo.value).then((res:any)=>{
            if(res.success){
                message.info("保存成功")
                if(callback) callback()
            }else{
                message.info(res.msg)
            }
        })
    }

    /**
     * 查询车辆 位置&施工信息
     * @param deviceId
     */
    const getDeviceLocationAndBuild = (deviceId:number)=>{
        waterInfo.value = new WaterInfo()
        return new Promise((resolve,reject)=>{
            axios.post("/water/findDeviceLocationAndBuild",{deviceId:deviceId}).then((res:any)=>{
                if(res.success){
                    Object.assign(waterInfo.value,res.data)
                    resolve(res)
                }else{
                    reject(false)
                    message.info(res.msg)
                }
            })
        })
    }

    return {
        waterViewInfo,
        waterFormState,
        waterInfo,
        waterList,
        getWaterInfo,
        setWaterInfo,
        getWaterList,
        getDeviceLocationAndBuild
    }
}